package com.salim.leetcode.$160;

public class IntersectionOfTwoLinkedLists {
    //第一种方法 双指针 先找到长链 然后找到长链上 长链长度-短链长度那个点 开始同时遍历长短链比较长度
    //第二种方法 三条线可以看做 a c b 两条路线 a+c+b = b+c+a 也就是说 a+c链走完后走b链 b+c链走完后走a链 他们会在c链起点会和
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pA = headA;
        ListNode pB = headB;
        while (pA != pB) {
            pA = pA == null ? headB : pA.next;
            pB = pB == null ? headA : pB.next;
        }
        return pA;
        // Note: In the case lists do not intersect, the pointers for A and B
        // will still line up in the 2nd iteration, just that here won't be
        // a common node down the list and both will reach their respective ends
        // at the same time. So pA will be NULL in that case.
    }

    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
}
